// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 * Description: Cpuhp specific symbols
 * Author: Huawei OS Kernel Lab
 * Create: Thu Jun 30 16:34:03 2022
 */

#include <linux/smp.h>
#include <linux/printk.h>
#include <linux/cpumask.h>
#include <linux/rcutree.h>
#include <linux/rcupdate.h>
#include <linux/stop_machine.h>

extern void liblinux_bringup_cpu(unsigned int cpu);

int __cpu_up(unsigned int cpu, struct task_struct *idle)
{
	set_cpu_online(cpu, true);

	/*
	 * Cause LDK doesn't support `secondary_start_kernel`, so
	 * do some tricks here to simulate cpu online.
	 */
	rcu_cpu_starting(cpu);	/* Enables RCU usage on this CPU */
	cpumask_set_cpu(cpu, &cpus_booted_once_mask);
	stop_machine_unpark(cpu);

	liblinux_bringup_cpu(cpu);

	pr_notice("CPU%u: up\n", cpu);
	return 0;
}

int __cpu_disable(void)
{
	unsigned int cpu = smp_processor_id();

	set_cpu_online(cpu, false);
	pr_notice("CPU%u: disabled\n", cpu);
	return 0;
}

void save_boot_affinity(unsigned long *boot_affinity, unsigned int bind_cpu)
{
	int ret;

	ret = liblinux_pal_thread_getaffinity(NULL, boot_affinity);
	BUG_ON(ret < 0);

	ret = liblinux_pal_thread_setaffinity(NULL,
			*(unsigned long *)cpumask_bits(cpumask_of(bind_cpu)));
	BUG_ON(ret < 0);
}

void restore_boot_affinity(unsigned long boot_affinity)
{
	int ret;

	ret = liblinux_pal_thread_setaffinity(NULL, boot_affinity);
	BUG_ON(ret < 0);
}

